var combinationSum = function(candidates, target) {
  const ans = [], path = [];
  candidates.sort((a, b) => a - b);
  backtracking(0, 0, candidates, target, ans, path);

  return ans;

};

function backtracking(startIndex, sum, candidates, target, ans, path) {
  if (sum === target) {
    ans.push([...path]);
    return;
  }

  for (let i = startIndex; i < candidates.length; i++) {
    if (sum + candidates[i] > target) return;
    path.push(candidates[i]);
    backtracking(i, sum + candidates[i], candidates, target, ans, path);
    path.pop();
  }
}

const candidates = [2,3,6,7], target = 7;
console.log(combinationSum(candidates, target))